package leetcode.part2;

import org.junit.Test;

import java.util.Arrays;

/** 167. 两数之和 II - 输入有序数组
 * <a href="https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/description/?envType=study-plan-v2&envId=top-interview-150">...</a>
 */
public class S167 {
    public int[] twoSum(int[] numbers, int target) {
        int len = numbers.length;
        int left = 0;
        int right = len-1;
        while (left < right) {
            int sum = numbers[left] + numbers[right];
            if (sum<target){
                left++;
            }else if (sum>target){
                right--;
            }else {
                return new int[]{left+1,right+1};
            }
        }
        return new int[2];
    }

    @Test
    public void test() {
        int[] numbers={2,7,11,15};
        int target = 9;
        int[] numbers1={2,3,4};
        int target1 = 6;
        int[] numbers2={2,3,4};
        int target2 = 6;
        System.out.println(Arrays.toString(twoSum(numbers,target)));
        System.out.println(Arrays.toString(twoSum(numbers1,target1)));
        System.out.println(Arrays.toString(twoSum(numbers2,target2)));

    }
}
